#!/usr/bin/python
#-*- coding: latin-1 -*-

from reverend import thomas
from os.path import isfile

class Bayes(thomas.Bayes):
	def train_char(self, text):
		if len(text):
			char = list(text)
			for i in range(len(char) -1):
				self.train(char[i + 1], char[i])
	
	def train_word(self, text):
		if len(text):
			words = text.split()
			for word in words:
				char = list(word)
				half = ''
				for i in range(len(char) -1):
					half += char[i]
					self.train(char[i + 1], half)
	
	def train_phrase(self, text):
		if len(text):
			phrases = text.split(".")
			for phrase in phrases:
				char = list(phrase)
				half = ''
				for i in range(len(char) -1):
					half += char[i]
					self.train(char[i + 1], half)
	
	def train_full(self, text):
		self.train_char(text)
		self.train_word(text)
		self.train_phrase(text)
		
#p = PasqualeSQL()
#
#to_learn = '/tmp/to_learn'
#if isfile(to_learn):
#	fd = open(to_learn, 'r')
#	for line in fd:
#		p.train_full(line)

#f_words = 'g_words.bay'
#f_parts = 'g_parts.bay'
#f_chars = 'g_chars.bay'

#g_words = Bayes()
#g_parts = Bayes()
#g_chars = Bayes()

#if isfile(f_words):
#	g_words.load('g_words.bay')

#if isfile(f_parts):
#	g_parts.load('g_parts.bay')

#if isfile(f_chars):
#	g_chars.load('g_chars.bay')

#guesser.train('english', 'the it she he they them are were to')
#guesser.guess('they went to el cantina')
#guesser.save('my_guesser.bay')
